<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/Public/BeginnerAdmin/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/Public/BeginnerAdmin/plugins/font-awesome/css/font-awesome.min.css">
</head>

<body>
<div style="margin: 15px;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新增文章</legend>
    </fieldset>

    <form class="layui-form" action="/home/article/edit_callback" method="POST" id="form1" enctype="multipart/form-data" >
        <input type="hidden" id="id" name='id' value="{$row.id}" />
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                     value="{$row.title}"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">作者</label>
            <div class="layui-input-block">
                <input type="text" name="author" lay-verify="required" autocomplete="off" placeholder="请输入作者"
                       value="{$row.author}"  class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">上传图片</label>
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe62f; </i>上传图片
            </button>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">&nbsp;</label>
            <img src="{$row.pic}"  width="400" height="300"  id="pic" />
        </div>
        <input type="hidden"  name="pic" value="{$row.pic}" />

        <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="0">写作</option>
                    <option value="1">阅读</option>
                    <option value="2">游戏</option>
                    <option value="3">音乐</option>
                    <option value="4">旅行</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-inline">
                <input type="text" name="score" lay-verify="score" placeholder="1-100分值越大排序越靠前" value="1"
                       value="{$row.score}"   autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">请填写正整数</div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">关键描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="keywords" lay-verify="required">{$row.keywords}</textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="content">{$row.content}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">日期</label>
                <div class="layui-input-block">
                    <input type="text" name="datetime" id="date" lay-verify="datetime" placeholder="yyyy-mm-dd H:i:s"
                           value="{$row.datetime}"   autocomplete="off" class="layui-input" >
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1" id="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/Public/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/Public/jquery/jquery.form.js"></script>
<script src="/Public/layui/dist/layui.all.js"></script>
<script>
    function ajaxCall()
    {
        window.parent.location.href=window.parent.location.href;
    }

</script>
<script>
    layui.use(['form', 'layedit','laydate','upload','jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var $ = layui.jquery;


        laydate.render({
            elem: '#date', //指定元素
            type: 'datetime'
        });

        layedit.set({
            uploadImage: {
                url: 'http://www.layui.test/home/article/uploadImage' //接口url
                ,type: 'POST'
            }
        });
        var editIndex = layedit.build('content' ,{height: 1000});


        //图片上传
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: 'http://www.layui.test/home/article/uploadImage' //上传接口
            ,type: 'POST'
            ,done: function(res){
                //上传完毕回调
                $("img").attr({ src: res.data.src, alt: res.data.title });
                $('input[name=pic]').val(res.data.src);
            }
            ,error: function(){
                //请求异常回调
            }
        });

        //前端自定义验证
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            },
            score: [/[0-9]{1,3}$/, '排序值1-100'],
            content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交 返回json数据
        layui.form.on('submit(demo1)', function (data) {
//            layer.alert(JSON.stringify(data.field), {
//                title: '最终的提交信息'
//            });


            //可选项
            var options = {
                beforeSubmit: showRequest,  //提交前的回调函数
                success: showResponse,      //提交后的回调函数
                error:showError,           //提交失败后回调函数
                timeout: 10000             //限制请求的时间，当请求大于10秒后，跳出请求
            }
            function showRequest(formData, jqForm, options){}
            function showResponse(responseText, statusText)
            {
                var error=responseText.error_code;
                var msg=responseText.msg;
                if(!msg){msg='网络异常,请稍候再试!';}
                if(error!='0'){layer.alert(msg, {icon: 2});return;}
                if(error == 0){
                    layer.msg(msg, {icon: 1},function(){
                        ajaxCall();
                    });
                    return;
                }
            }
            function showError(){
                var msg='网络异常,请稍候再试!';
                layer.alert(msg);
            }

            $('#form1').ajaxSubmit(options);
            return false;

        });


    });
</script>

</body>

</html>